package com.jr.controller;

import com.jr.mapper.ProviderServiceMapper;
import com.jr.pojo.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/provider-services")
@CrossOrigin(origins = "*")
public class ProviderServiceController {

    @Autowired
    private ProviderServiceMapper providerServiceMapper;

    /**
     * 获取所有服务商服务关联数据
     */
    @GetMapping("/all")
    @ResponseBody
    public Map<String, Object> getAllProviderServices() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<ProviderService> providerServices = providerServiceMapper.selectAllWithDetails();
            result.put("success", true);
            result.put("providerServices", providerServices);
            result.put("count", providerServices.size());
            result.put("message", "成功获取所有服务商服务关联数据");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取服务商服务关联数据失败: " + e.getMessage());
            System.err.println("获取服务商服务关联数据失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 根据服务商ID获取服务关联数据
     */
    @GetMapping("/provider/{providerId}")
    @ResponseBody
    public Map<String, Object> getProviderServicesByProviderId(@PathVariable Integer providerId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<ProviderService> providerServices = providerServiceMapper.selectByProviderIdWithDetails(providerId);
            result.put("success", true);
            result.put("providerServices", providerServices);
            result.put("count", providerServices.size());
            result.put("message", "成功获取服务商服务关联数据");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取服务商服务关联数据失败: " + e.getMessage());
            System.err.println("获取服务商服务关联数据失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 根据服务类型ID获取服务关联数据
     */
    @GetMapping("/service-type/{serviceTypeId}")
    @ResponseBody
    public Map<String, Object> getProviderServicesByServiceTypeId(@PathVariable Integer serviceTypeId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<ProviderService> providerServices = providerServiceMapper.selectByServiceTypeId(serviceTypeId);
            result.put("success", true);
            result.put("providerServices", providerServices);
            result.put("count", providerServices.size());
            result.put("message", "成功获取服务类型关联数据");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取服务类型关联数据失败: " + e.getMessage());
            System.err.println("获取服务类型关联数据失败: " + e.getMessage());
        }
        return result;
    }
}
